package org.example.afiona.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.example.afiona.VO.SentinelOrderInfoVO;
import org.example.afiona.common.VO.ResultVO;
import org.example.afiona.common.utils.ResultVOUtil;
import org.example.afiona.service.SentinelOrderFeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: leo
 * @date: 2021/6/11
 */
@RestController
@RequestMapping("user")
@RefreshScope
public class UserController {

    @Resource
    private RestTemplate restTemplate;

    private static final String INVOKE_URL = "http://provider-nacos-order";

    @Autowired
    private SentinelOrderFeignApi sentinelOrderFeignApi;

    @Value("${info}")
    private String info;

    @GetMapping("/info")
    public String getInfo() {
        return info;
    }

    @GetMapping("status")
    public ResultVO<Integer> getStatus() {
        ResultVO<Integer> resultVO = restTemplate.getForObject(INVOKE_URL + "/order/status", ResultVO.class);
        return resultVO;
    }

    @GetMapping("limit")
//    @SentinelResource(value = "testLimit", blockHandler = "handleLimit")
    @SentinelResource(value = "testLimit", blockHandlerClass = CustomBlockHandler.class, blockHandler = "handleException")
    public ResultVO testLimit() {
        return ResultVOUtil.success();
    }

    public ResultVO handleLimit(BlockException ex) {
        return ResultVOUtil.error(405, "被限制了哦");
    }

    @GetMapping("order/list")
    public ResultVO<List<SentinelOrderInfoVO>> list() {
        return sentinelOrderFeignApi.getOrderListByUserId(1);
    }

}
